#include"LinkedList.h"
#include <stdio.h>
#include <stdlib.h>


//------------------------------------------
//function about listdata     --------------
//------------------------------------------


void Init(ListData* data)
{
    scanf("%d%f",&data->a,&data->b);
}

int Judge(ListData* data,ListData* comData)
{
   if(data->a==comData->a&&data->b==comData->b)
   { 
        return 1;
   }
   else {
   return 0;
   }
}

void DataCopy(ListData* des,ListData* res)
{
   des->a=res->a;
   des->b=res->b;
}


void show(ListData* data)
{
    printf("%d %f\n",data->a,data->b);
}


//------------------------------------------
//function about linkedlist   --------------
//------------------------------------------

LinkedNode* InitLinkedList()       //init list
{
    LinkedNode* list=(LinkedNode*)malloc(sizeof(LinkedNode));
    list->prior=list;
    list->next=list;
    return list;
}


int Ifempty(LinkedNode* list)
{
   if(list->next==list->prior)
   {
    return 0; //empty
   }
   else {
   return 1; //not empty
   }
 
}


void DestroyList(LinkedNode* list)      //destroy list
{
  LinkedNode* tmp=list;
    LinkedNode* tmp2=list;
    for(int i=0;i<GetLength(list);i++)
    {
        tmp=tmp->next;
        tmp2=tmp;
        free(tmp2);
    }
   
   free(list);

}


void ClearList(LinkedNode* list)
{
    LinkedNode* tmp=list;
    LinkedNode* tmp2=list;
    for(int i=0;i<GetLength(list);i++)
    {
        tmp=tmp->next;
        tmp2=tmp;
        free(tmp2);
    }
   
   list->prior=list;
   list->next=list;
}


int GetLength(LinkedNode* list)
{
  int i=0;
  LinkedNode* tmp=list;
  while(tmp->next!=list)
  {
    tmp=tmp->next;
    i++;
  }
  return i;
}

ListData* GetElem(LinkedNode* list,int location)
{
    LinkedNode* tmp=list;
    for(int i=0;i<GetLength(list);i++)
    {
       tmp=tmp->next;
    }
    
    return &(tmp->data);


}

ListData GetElemCpy(LinkedNode* list,int location)
{
   LinkedNode* tmp=list;
    for(int i=0;i<GetLength(list);i++)
    {
       tmp=tmp->next;
    }
    
    return tmp->data;

}


int FindElem(LinkedNode* list,ListData* elem)  //return the location number in list.you can replace it by iterator in future
{
   LinkedNode* tmp=list;
    for(int i=0;i<GetLength(list);i++)
    {
        tmp=tmp->next;
        if(Judge(&(tmp->data), elem))
        {
            return i;
        }
    }
    return 0; //don`t find equal elem

}

void Insert(LinkedNode* list,ListData* elem,int location)
{
  //get the node at location
    LinkedNode* newElem=(LinkedNode*)malloc(sizeof(LinkedNode));
    LinkedNode* tmp=list;
    for(int i=0;i<location;i++)
    {
        tmp=tmp->next;
    }

    //change data and pointer
    DataCopy(&(newElem->data), elem);
    newElem->next=tmp;
    newElem->prior=tmp->prior;

    newElem->prior->next=newElem;
    newElem->next->prior=newElem;

}

void Delet(LinkedNode* list,int location)
{
    LinkedNode* tmp=list;
    for(int i=0;i<location;i++)
    {
        tmp=tmp->next;
    }

    tmp->prior->next=tmp->next;
    tmp->next->prior=tmp->prior;

    free(tmp);
}

void PushBack(LinkedNode* list,ListData* elem)
{
  LinkedNode* newNode=(LinkedNode*)malloc(sizeof(LinkedNode));
    DataCopy(&(newNode->data), elem);     //copy data


    //chang pointer
    newNode->next=list;
    newNode->prior=list->prior;

    list->prior=newNode;
    newNode->prior->next=newNode;
}

void PushFront(LinkedNode* list,ListData* elem)
{
    LinkedNode* newNode=(LinkedNode*)malloc(sizeof(LinkedNode));
    DataCopy(&(newNode->data), elem);     //copy data

    newNode->prior=list;
    newNode->next=list->next;

    list->next->prior=newNode;
    list->next=newNode;
}

void ShowData(LinkedNode* list)
{
    LinkedNode* tmp=list;
   for(int i=0;i<GetLength(list);i++)
   {
    printf("the %d data is:\n",i+1);
    show(&(tmp->next->data));
    tmp=tmp->next;
   }
}
